import createRouteMap from './create-route-map'
import createRoute from './utils/route'

export default function createMatcher (routes) {
  // 获取 pathList 和 pathMap
  const { pathList, pathMap } = createRouteMap(routes)
  function match (path) {
    const record = pathMap[path]
    if (record) {
      // 创建一个路由对象，里面记录路径所对应的内容
      // route对象 --> { matched: [musicRecord], path: '/music' }
      // route对象 --> { matched: [musicRecord, popRecord], path: '/music/pop' }
      return createRoute(record, path)
    }
    return createRoute(null, path)
  }

  function addRoutes (routes) {
    createRouteMap(routes, pathList, pathMap)
  }

  return {
    match,
    addRoutes
  }
}
